package com.lijin.api.controller;

import com.lijin.api.LotteryInformationControllerApi;
import com.lijin.domain.dto.LotteryInformationDto;
import com.lijin.domain.enums.LotteryCodeEnums;
import com.lijin.domain.query.LotteryInformationQuery;
import com.lijin.domain.query.base.Page;
import com.lijin.service.LotteryInformationService;
import io.swagger.annotations.Api;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by gold-jx on 2017/12/8.
 */
@RestController
@Api(description = "LOTTERY -> 彩票管理")
public class LotteryInformationController implements LotteryInformationControllerApi{
    @Autowired
    private LotteryInformationService lotteryInformationService;

    @Override
    public ResponseEntity<Page<LotteryInformationDto>> findInfoByCode(LotteryInformationQuery lotteryInformationQuery) {
        Page<LotteryInformationDto> lotteryInformationDtoPage=lotteryInformationService.findInformationByCode(lotteryInformationQuery);
        if (!StringUtils.isEmpty(lotteryInformationDtoPage.getDatas())) {
            List<LotteryInformationDto> lotteryInformationDtos = lotteryInformationDtoPage.getDatas().stream().map(request -> {
                LotteryInformationDto lotteryInformationDto=new LotteryInformationDto();
                lotteryInformationDto.setId(request.getId());
                lotteryInformationDto.setLotteryCode(request.getLotteryCode());
                lotteryInformationDto.setLotteryExpect(request.getLotteryExpect());
                lotteryInformationDto.setOpenCode(request.getOpenCode());
                lotteryInformationDto.setOpenTime(request.getOpenTime());
                lotteryInformationDto.setCreateTime(request.getCreateTime());
                lotteryInformationDto.setState(request.getState());
                lotteryInformationDto.setVersion(request.getVersion());
                lotteryInformationDto.setSource(request.getSource());
                return lotteryInformationDto;
            }).collect(Collectors.toList());
            return ResponseEntity.ok(new Page<>(lotteryInformationDtoPage.getTotalCount(), lotteryInformationDtos));
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);

    }

    @Override
    public ResponseEntity<List<LotteryInformationDto>> findAllNewInfo() {
        List<LotteryInformationDto> lotteryInformationDtos = lotteryInformationService.findAllFirst();
        if(lotteryInformationDtos !=null){
            return ResponseEntity.ok(lotteryInformationDtos);
        }


        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Override
    public ResponseEntity<LotteryInformationDto> findInfoByCodeAndExpect(LotteryCodeEnums code, String expect) {
        LotteryInformationDto lotteryInformationDto = lotteryInformationService.findInformationByCodeAndExpect(code.toString(), expect);
        if(lotteryInformationDto !=null){
            return ResponseEntity.ok(lotteryInformationDto);
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
